class Solution {
public:
    vector<vector<int>> ans;
    vector<int> res;
    void dfs(vector<int>& candidates,int target,int begin,int sum){
      if(sum == target){
        ans.push_back(res);
        return;
      }
      if(sum > target){
        return;
      }
      for(int i = begin;i < candidates.size();i++){
        if(i > begin && candidates[i] == candidates[i-1])
                continue;

        sum += candidates[i];
        res.push_back(candidates[i]);
        dfs(candidates,target,i+1,sum);

        sum -= candidates[i];
        res.pop_back();
      }
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
      sort(begin(candidates),end(candidates));
      dfs(candidates,target,0,0);
      return ans;
    }
};
